package com.company.project.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.project.common.utils.DataResult;
import com.company.project.entity.CategoryMagEntity;
import com.company.project.service.CategoryMagService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;



/**
 * 商品类别
 *
 * @author HKX
 * @email *****@mail.com
 * @date 2022-05-03 11:34:43
 */
@Controller
@RequestMapping("/")
public class CategoryMagController {
    @Autowired
    private CategoryMagService categoryMagService;


    /**
    * 跳转到页面
    */
    @GetMapping("/index/categoryMag")
    public String categoryMag() {
        return "categorymag/list";
        }

    @ApiOperation(value = "新增")
    @PostMapping("categoryMag/add")
    @RequiresPermissions("categoryMag:add")
    @ResponseBody
    public DataResult add(@RequestBody CategoryMagEntity categoryMag){
        categoryMagService.save(categoryMag);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("categoryMag/delete")
    @RequiresPermissions("categoryMag:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        categoryMagService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("categoryMag/update")
    @RequiresPermissions("categoryMag:update")
    @ResponseBody
    public DataResult update(@RequestBody CategoryMagEntity categoryMag){
        categoryMagService.updateById(categoryMag);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("categoryMag/listByPage")
    @RequiresPermissions("categoryMag:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody CategoryMagEntity categoryMag){
        Page page = new Page(categoryMag.getPage(), categoryMag.getLimit());
        LambdaQueryWrapper<CategoryMagEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        //queryWrapper.eq(CategoryMagEntity::getId, categoryMag.getId());
        if(StrUtil.isNotBlank(categoryMag.getGoodsCategory())){
            queryWrapper.like(CategoryMagEntity::getGoodsCategory,categoryMag.getGoodsCategory());
        }
        queryWrapper.eq(CategoryMagEntity::getDeleted,1);
        IPage<CategoryMagEntity> iPage = categoryMagService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }



}
